package cn.dida.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.GeneralSet;
import cn.dida.model.Pageable;
import cn.dida.model.Staff;
import cn.dida.model.Sysres;
import cn.dida.util.MD5Util;

/**
 * 账户管理 数据交互类
 * @author weiwei
 */
@Repository
public class GeneralSetDao extends DaoBase {

    public List<GeneralSet> query(Integer companyId) {
        Object[] args = {};
        StringBuffer sqlQueryResult = new StringBuffer("select gen.* from pub_generalsettings gen join company cpn on gen.company_id = cpn.id");

        StringBuffer sqlWhere = new StringBuffer(" where gen.setupstat = 1 and  gen.company_id = ? ");
        args = ArrayUtils.add(args, companyId);


        // 查询记录
        sqlQueryResult.append(sqlWhere).append("  order by gen.id asc");

        List<GeneralSet> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(GeneralSet.class));
        return result;
    }
    
    public boolean insertGeneralSet (GeneralSet generalset){
    	
    	 String sql = "insert into pub_generalsettings(company_id, setupname, setupvalue, setupdescribe) values (:company_id, :setupname, :setupvalue, :setupdescribe)";
         
    	 HashMap<String, Object> paramMap = new HashMap<>();
         paramMap.put("company_id", generalset.getCompany_id());
         paramMap.put("setupname", generalset.getSetupname());
         paramMap.put("setupvalue", generalset.getSetupvalue());
         paramMap.put("setupdescribe", generalset.getSetupdescribe());
         
         int result = namedParameterJdbcTemplate.update(sql, paramMap);
         
         return  result == 1;
    	
    }
   
    public GeneralSet getGeneralSet(Integer companyId, Integer id){
        String sql = "select gen.* from pub_generalsettings gen join company cpn on gen.company_id = cpn.id and  gen.company_id = ? and gen.id = ?";
        try {
        	GeneralSet generalset = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<GeneralSet>(GeneralSet.class), companyId ,id );
            return generalset;
        } catch (DataAccessException e) {
            return null;
        }
    }
    
    public boolean updateGeneralSet(GeneralSet generalset){
    	
    	String sql = "update pub_generalsettings set company_id=:company_id,  setupvalue=:setupvalue where company_id = :company_id and id = :id";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", generalset.getId());
        paramMap.put("company_id", generalset.getCompany_id());
        paramMap.put("setupvalue", generalset.getSetupvalue());
        int result =namedParameterJdbcTemplate.update(sql, paramMap);
    	return result == 1;
    }
    
    public boolean deleteGeneralSet(Integer companyId, Integer id) {
        String sql = "delete from pub_generalsettings where company_id = ? and id = ?";
        return jdbcTemplate.update(sql, companyId, id) == 1;
    }
    
    public List<Map<String, Object>> queryGeneralSetExport(Integer companyId) {
		Object[] args = {};
		StringBuffer sqlQueryResult = new StringBuffer("select gen.* from pub_generalsettings gen join company cpn on gen.company_id = cpn.id ");
		
		StringBuffer sqlWhere = new StringBuffer(" where gen.setupstat = 1 and gen.company_id = ?  ");
		args = ArrayUtils.add(args, companyId);
		sqlQueryResult.append(sqlWhere);
		List<Map<String,Object>> result = jdbcTemplate.queryForList(sqlQueryResult.toString(), args);
		
		return result;
		
	}
    
    public Map<String, Object> queryGeneralSet(Integer companyid){
		// TODO Auto-generated method stub
		//取通用设置公众号付款内容
		String sql ="select setupvalue from pub_generalsettings where setupcode=2 and company_id =?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql,companyid);
		if(map!=null){
			return  map;
		}
		return  null;
	}
}
